import java.util.StringTokenizer;


public abstract class MovingObject extends SpaceObject
{
	public static final int FRAME_RATE = AsteroidsGame.FRAME_RATE;
	
	
	protected double vy, vx; // in pixels per second
	//-Hit Sound
	//-Vulnerability // list of other classes?
	
	public MovingObject(int x, int y)
	{
		super(x,y);
		vy = vx = 0;
	}
	
	public MovingObject()
	{
		super();
	}
	
	public double getVX() { return vx; }
	public double getVY() { return vy; }
	
	// returns magnitude of absolute speed
	public double getSpeed()
	{
		return Math.sqrt(vx*vx + vy*vy);
	}
	
	protected void loadState(StringTokenizer strtok) throws NumberFormatException
	{
		super.loadState(strtok);
		vx = Double.parseDouble(strtok.nextToken());
		vy = Double.parseDouble(strtok.nextToken());
	}
	protected String saveState()
	{
		return super.saveState() + String.format("%f %f ", vx, vy);
	}
	
	// size of the image, to be used for determining when to wrap around the screen
	// these will in general be slightly smaller than the actual image size
	public abstract int getWrapWidth();
	public abstract int getWrapHeight();

	public void move()
	{
		// amount that the object can go off screen before wrapping
		int xBuffer = getWrapWidth()/2; // we don't want to wrap until the image is completely off the screen
		int yBuffer = getWrapHeight()/2; 
		
		x += vx/FRAME_RATE;
		y += vy/FRAME_RATE;
		if(x > SCREEN_WIDTH + xBuffer)
			x = -xBuffer;
		else if(x < -xBuffer)
			x = SCREEN_WIDTH + xBuffer;
		
		if(y > SCREEN_HEIGHT + yBuffer)
			y = -yBuffer;
		else if(y < -yBuffer)
			y = SCREEN_HEIGHT + yBuffer;
	}

	static public int sign (double d)
	{
		if(d < 0) return -1;
		if(d > 0) return 1;
		return 0;
	}
}
